package com.sicheng.lc.杂题.多路归并技巧之构造有序链表;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/12 15:45
 */
public class 第K个最小的素数分数 {
    //https://leetcode.cn/problems/k-th-smallest-prime-fraction/
    public int[] kthSmallestPrimeFraction(int[] nums, int k) {
        PriorityQueue<int[]> heap = new PriorityQueue<>(nums.length,
                (a, b) -> nums[a[0]] * nums[b[1]] - nums[a[1]] * nums[b[0]]
        );
        for (int j = 1; j < nums.length; j++) {
            heap.offer(new int[]{0, j});
        }
        int[] a = null;
        for (int m = 0; m < k; m++) {
            if (heap.isEmpty())
                break;
            a = heap.poll();
            if (m + 1 == k)
                break;
            if (a[0] + 1 < a[1]) {
                a[0]++;
                heap.offer(a);
            }
        }
        a[0] = nums[a[0]];
        a[1] = nums[a[1]];
        return a;
    }

    public static void main(String[] args) {

        第K个最小的素数分数 s = new 第K个最小的素数分数();
        int[] nums = {1, 2, 3, 5};
        int k = 3;
        System.out.println(Arrays.toString(s.kthSmallestPrimeFraction(nums, k)));
    }

}
